<template>
  <div class="content">
    <lt-table
      :data="formData"
      v-bind="pageContentConfig"
      :listCount="dateCount"
      v-model:page="pageInfo"
    >
      <template #header>
        <el-button type="primary" v-if="isCreate" @click="dialogVisibleClick">{{
          contentTitle
        }}</el-button>
      </template>
      <template #enable="scope">
        <el-button
          size="mini"
          :type="scope.row.enable ? 'success' : 'danger'"
          >{{ scope.row.enable ? '启用' : '禁用' }}</el-button
        >
      </template>
      <template #createAt="scope">
        {{ $filters.formatTime(scope.row.createAt) }}
      </template>
      <template #updateAt="scope">
        {{ $filters.formatTime(scope.row.updateAt) }}
      </template>

      <template #handler="scope">
        <span
          ><el-button
            v-if="isUpdate"
            icon="el-icon-edit"
            type="text"
            size="mini"
            @click="updateClick(scope.row)"
            >编辑</el-button
          ></span
        >
        <span
          ><el-button
            v-if="isDelete"
            icon="el-icon-delete"
            type="text"
            size="mini"
            @click="deleteData(scope.row)"
            >删除</el-button
          ></span
        >
      </template>
      <template
        v-for="slot in otherPropsSlots"
        :key="slot.prop"
        #[slot.slotName]="scope"
      >
        <template v-if="slot.slotName">
          <slot :name="slot.slotName" :row="scope.row"></slot>
        </template>
      </template>
    </lt-table>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref, watch } from 'vue'
import { useStore } from 'vuex'
import { ltTable } from '../../table/index'
import { userPermission } from '../../../hooks/user-permission'
export default defineComponent({
  components: {
    ltTable
  },
  props: {
    pageContentConfig: {
      type: Object
    },
    pageName: {
      type: String,
      default: () => {
        return ''
      }
    },
    contentTitle: {
      type: String,
      default: '新建用户'
    }
  },
  emits: ['visibleClick', 'pageUpdateClick'],
  setup(props, { emit }) {
    const store = useStore()
    //权限

    const isCreate = userPermission(props.pageName, 'create')
    const isUpdate = userPermission(props.pageName, 'update')
    const isDelete = userPermission(props.pageName, 'delete')
    const isQuery = userPermission(props.pageName, 'query')

    const pageInfo = ref({ currentPage: 0, pageSize: 10 })
    const getPageData = (queryInfo: any = {}) => {
      if (!isQuery) return
      store.dispatch('system/getPageListAction', {
        pageName: props.pageName,
        queryInfo: {
          offset: pageInfo.value.currentPage,
          size: pageInfo.value.pageSize,
          ...queryInfo
        }
      })
    }
    getPageData()
    const formData = computed(() =>
      store.getters['system/getListData'](props.pageName)
    )
    const dateCount = computed(() =>
      store.getters['system/getListCount'](props.pageName)
    )

    watch(pageInfo, () => {
      getPageData()
    })
    const otherPropsSlots = props.pageContentConfig?.pageDatas.filter(
      (item: any) => {
        if (item.slotName === 'enable') return false
        if (item.slotName === 'createAt') return false
        if (item.slotName === 'updateAt') return false
        if (item.slotName === 'handler') return false
        return true
      }
    )

    //删除
    const deleteData = (item: any) => {
      store.dispatch('system/deletePageList', {
        pageName: props.pageName,
        id: item.id
      })
      //console.log(item)
    }
    const dialogVisible = ref(true)
    const dialogVisibleClick = () => {
      emit('visibleClick')
    }
    //修改
    const updateClick = (item: any) => {
      emit('pageUpdateClick', item)
    }
    return {
      formData,
      getPageData,
      deleteData,

      dateCount,
      pageInfo,
      otherPropsSlots,
      userPermission,
      isCreate,
      isUpdate,
      isDelete,
      dialogVisible,

      dialogVisibleClick,
      updateClick
    }
  }
})
</script>

<style scoped>
.content {
  border-top: 20px solid #f5f5f5;
}
</style>
